package com.nk.iexam.adapter;

import java.util.List;

import com.nk.iexam.R;
import com.nk.iexam.ds.SubjectDataSource;
import com.nk.iexam.models.Subject;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubjectAdapter extends ArrayAdapter<Subject>
{
	private Context _context;
	private int _layoutResourceId;  
	private List<Subject> _data = null;

	public SubjectAdapter(Context context_, int layoutResourceId_, List<Subject> data_) {
		super(context_, layoutResourceId_, data_);
		this._layoutResourceId = layoutResourceId_;
		this._context = context_;
		this._data = data_;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		SubjectHolder holder = null;

		if(row == null)
		{
			LayoutInflater inflater = ((Activity)_context).getLayoutInflater();
			row = inflater.inflate(_layoutResourceId, parent, false);

			holder = new SubjectHolder();
			holder.imgIcon = (ImageView)row.findViewById(R.id.list_image);
			holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

			row.setTag(holder);
		}
		else
		{
			holder = (SubjectHolder)row.getTag();
		}

		Subject subject = _data.get(position);
		holder.txtTitle.setText(subject.getName());

		switch ((int)subject.getId()) {
		case SubjectDataSource.SUBECT_ID_MATH:
			holder.imgIcon.setImageResource(R.drawable.math);
			break;
		case SubjectDataSource.SUBECT_ID_LITTERATURE:
			holder.imgIcon.setImageResource(R.drawable.litterature);
			break;
		case SubjectDataSource.SUBECT_ID_ENGLISH:
			holder.imgIcon.setImageResource(R.drawable.english);
			break;
		case SubjectDataSource.SUBECT_ID_PHYSICS:
			holder.imgIcon.setImageResource(R.drawable.atom);
			break;
		case SubjectDataSource.SUBECT_ID_CHEMISCAL:
			holder.imgIcon.setImageResource(R.drawable.chemistry);
			break;
		case SubjectDataSource.SUBECT_ID_BIOLOGY:
			holder.imgIcon.setImageResource(R.drawable.biology);
			break;
		case SubjectDataSource.SUBECT_ID_HISTORY:
			holder.imgIcon.setImageResource(R.drawable.history);
			break;
		case SubjectDataSource.SUBECT_ID_GEOGRAPHIC:
			holder.imgIcon.setImageResource(R.drawable.geographic);
			break;
		default:
			break;
		}
		
		return row;
	}

	static class SubjectHolder
	{
		ImageView imgIcon;
		TextView txtTitle;
	}
}
